package com.learnyou.server.xingzuo.dao.model;

import com.learnyou.server.xingzuo.common.constants.BaseConstants;
import com.learnyou.server.xingzuo.dto.resultDTO.LoginResultDTO;
import com.learnyou.server.xingzuo.dto.resultDTO.wechat.WechatBindingUserDTO;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 用来保存线程请求的上下文信息
 */
public class WechatContextUtil {

    private static final ThreadLocal<Map<String, Object>> holder = new ThreadLocal<Map<String, Object>>();

    public static Map<String, Object> getThreadContext() {
        Map<String, Object> context = holder.get();
        if (null == context) {
            context = new HashMap<>();
            holder.set(context);
        }
        return context;
    }

    public static void clear() {
        getThreadContext().clear();
    }

    public static void remove() {
        holder.remove();
    }


    public static void setWechatUser(WechatBindingUserDTO wechatBindingUserDTO){
        getThreadContext().put(BaseConstants.USER, wechatBindingUserDTO);
    }

    public static WechatBindingUserDTO getWechatUser(){
        Object holderObj = getThreadContext().get(BaseConstants.USER);
        if (Objects.nonNull(holderObj)) {
            return (WechatBindingUserDTO) holderObj;
        } else {
            return null;
        }
    }

}
